package com.zzxx.controller;



import com.zzxx.entity.Orders;
import com.zzxx.entity.QrInfo;
import com.zzxx.entity.Ticket;
import com.zzxx.entity.User;
import com.zzxx.exceptions.QrException;
import com.zzxx.service.TicketService;
import com.zzxx.utils.QrCodeCreateUtils;
import org.apache.commons.io.FileUtils;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("qrcode")
public class QrCodeController {
	@Autowired(required = false)
	private HttpServletRequest request;

	@Autowired
	private TicketService ticketService;

	@RequestMapping("scan")
	public String uploadQr(MultipartFile uploadFile) throws IOException {
		String originalFilename = uploadFile.getOriginalFilename();
		//获取后缀
		String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
		//如果后缀时.jpg或者.png才解析，否则返回
		if (!".jpg".equals(suffix)&&!".png".equals(suffix)) return "noJpg";
		try {
			String message = ticketService.scanQr(uploadFile);
			ticketService.checkMessage(message,0);
			return  "success";
		} catch (QrException qr){
			return qr.getMessage();
		}catch (Exception e) {
			return "noQr";
		}
	}

	@RequestMapping(value = "code/{message}",method = RequestMethod.GET)
	public String code(@PathVariable("message") String message){
		try {
			ticketService.checkMessage(message,1);
			return "success";
		} catch (QrException e) {
			return e.getMessage();
		}
	}


	@RequestMapping(value = "qrInfo/{oid}",method = RequestMethod.GET)
	public QrInfo getQrInfo(@PathVariable("oid") String oid){
		QrInfo qrInfo = new QrInfo();
		ticketService.packageQrInfo(qrInfo,oid);
		return qrInfo;
	}
}
